import Vue from 'vue'
import Vuex from 'vuex'
import * as api from '../utils/api.js'
import all from './modules/all'
Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    mapData: [{
        name: "深圳市",
        province: "广东省福田区新园路37号",
        outboundAmount: "485.21",
        type: "ok",
        value: 0.02,
        dotid: 0,
        lat: 22.5455465546,
        lng: 114.0527779134,
      },
      {
        name: "广州市",
        province: "广东省越秀区吉祥路79号",
        outboundAmount: "485.21",
        type: "ok",
        value: 0.02,
        dotid: 1,
        lat: 23.1317146641,
        lng: 113.2595185241,
      },
      {
        name: "汕头市",
        province: "广东省越秀区吉祥路79号",
        outboundAmount: "485.21",
        type: "ok",
        value: 0.02,
        dotid: 1,
        lat: 23.356024358,
        lng: 116.6777689967,
      },
    ],
    vmware: {},
    hWStack: {},
    CloudInfo: {},
    countAll: {},
    phyResources: {
      hostCount: 0,
      storgeCount: 0,
      resourcePoolCount: 0,
      netWorkCount: 0,
    },
    resourcesUse: {
      "storgeCapacitySize": 0,
      "hostCpuCount": 0,
      "hostMemSize": 0,
      "vmCpuCount": 0,
      "vmDiskSize": 0,
      "vmMemSize": 0,
      "storgeFreeSize": 0
    },
    resourcesUsePercent: {

    },
    phyResourcesBar: [{
        type: "服务器",
        value: 0
      },
      {
        type: "网络设备",
        value: 0
      },
      {
        type: "储存",
        value: 0
      }, {
        type: "安全",
        value: 0
      },
      {
        type: "其他",
        value: 0
      },
    ],
    vdcAndProjectCount: {
      vdc: 0,
      project: 0
    },

    zjBusinessCount: {
      distributioncpu: 0,
      distributionstorage: 0,
      vmcount: 0,
      distributionmem: 0,
    }

  },
  mutations: {
    setCloudInfo(state, data) {
      state.CloudInfo = data
    },
    setHWStack(state, data) {
      state.hWStack = data
    },
    setVmware(state, data) {
      state.vmware = data
    },
    setCountAll(state, data) {
      state.countAll = data
    },
    setCountPhyResources(state, data) {
      state.phyResources = data
    },
    seteRsourcesUse(state, data) {
      state.resourcesUse = data
    },
    seteResourcesUsePercent(state) {
      let data = [{
          label: "CPU",
          percent: ((state.resourcesUse.hostCpuCount / state.countAll.vmCpuCount) * 100).toFixed(1),
          count: "1100/2000"
        },
        {
          label: "内存",
          percent: ((state.resourcesUse.hostMemSize / state.countAll.vmMemSize) * 100).toFixed(1),

          count: "1100/2000"
        },
        {
          label: "存储",
          percent: ((state.resourcesUse.storgeCapacitySize / state.countAll.vmDiskSize) * 100).toFixed(1),

          count: "1100/2000"
        },
        {
          label: "公网IP",
          percent: 60,
          count: "1100/2000"
        },
        {
          label: "出口宽带",
          percent: 60,
          count: "1100/2000"
        },
      ]
      state.resourcesUsePercent = data
    },
    setePhyResourcesBar(state) {
      let data = [{
          type: "服务器",
          value: state.phyResources.hostCount
        },
        {
          type: "网络设备",
          value: state.phyResources.netWorkCount

        },
        {
          type: "储存",
          value: state.phyResources.storgeCount

        }, {
          type: "安全",
          value: 0
        },
        {
          type: "其他",
          value: state.phyResources.resourcePoolCount

        },
      ]
      state.phyResourcesBar = data
    },
    setVdcAndProject(state, data) {
      state.vdcAndProjectCount = data
    },
    setZjBusinessQueryList(state, data) {
      state.zjBusinessCount = data
    },
  },
  actions: {
    setCount({
      commit
    }, data) {
      let count = {
        vmCount: 0,
        vmCpuCount: 0,
        vmDiskSize: 0,
        vmMemSize: 0
      }
      let phyResources = {
        hostCount: 0,
        storgeCount: 0,
        resourcePoolCount: 0,
        netWorkCount: 0,
      }
      let resourcesUse = {
        "storgeCapacitySize": 0,
        "hostCpuCount": 0,
        "hostMemSize": 0,
        "vmCpuCount": 0,
        "vmDiskSize": 0,
        "vmMemSize": 0,
        "storgeFreeSize": 0
      }

      data.map(i => {
        count.vmCount = i.cloudResources.vmCount + count.vmCount;
        count.vmCpuCount = i.cloudResources.vmCpuCount + count.vmCpuCount;
        count.vmDiskSize = i.cloudResources.vmDiskSize + count.vmDiskSize;
        count.vmMemSize = i.cloudResources.vmMemSize + count.vmMemSize;

        phyResources.hostCount = i.phyResources.hostCount + phyResources.hostCount;
        phyResources.storgeCount = i.phyResources.storgeCount + phyResources.storgeCount;
        phyResources.resourcePoolCount = i.phyResources.resourcePoolCount + phyResources.resourcePoolCount;
        phyResources.netWorkCount = i.phyResources.netWorkCount + phyResources.netWorkCount;

        resourcesUse.storgeCapacitySize = i.resourcesUse.storgeCapacitySize + resourcesUse.storgeCapacitySize;
        resourcesUse.hostCpuCount = i.resourcesUse.hostCpuCount + resourcesUse.hostCpuCount;
        resourcesUse.hostMemSize = i.resourcesUse.hostMemSize + resourcesUse.hostMemSize;
        resourcesUse.vmCpuCount = i.resourcesUse.vmCpuCount + resourcesUse.vmCpuCount;
        resourcesUse.vmMemSize = i.resourcesUse.vmMemSize + resourcesUse.vmMemSize;
        resourcesUse.storgeFreeSize = i.resourcesUse.storgeFreeSize + resourcesUse.storgeFreeSize;
      })


      commit('setCountAll', count)
      commit('setCountPhyResources', phyResources)
      commit('seteRsourcesUse', resourcesUse)
      commit('seteResourcesUsePercent')
      commit('setePhyResourcesBar')
      // console.log(resourcesUse)
    },
    getCloudInfo({
      commit
    }) {
      return new Promise((resolve, reject) => {
        api.getCloudInfo().then(res => {

          commit('setCloudInfo', res)
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },
    vmware({
      commit
    }) {
      return new Promise((resolve, reject) => {
        api.vmware().then(res => {
          commit('setVmware', res)
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },
    hWStack({
      commit
    }) {
      return new Promise((resolve, reject) => {
        api.hWStack().then(res => {
          commit('setHWStack', res)
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },
    vdcCount({
      commit
    }) {
      return new Promise((resolve, reject) => {
        api.vdcCount().then(res => {
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },
    projectCount({
      commit
    }) {
      return new Promise((resolve, reject) => {
        api.projectCount().then(res => {
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },
    zjBusinessQueryList({
      commit
    }) {
      return new Promise((resolve, reject) => {
        api.zjBusinessQueryList().then(res => {
          let count = {
            distributioncpu: 0,
            distributionstorage: 0,
            vmcount: 0,
            distributionmem: 0,
          }
          res.map(i => {
            count.distributioncpu = Number(i.distributioncpu) + count.distributioncpu
            count.distributionstorage = Number(i.distributionstorage) + count.distributionstorage
            count.vmcount = Number(i.vmcount) + count.vmcount
            count.distributionmem = Number(i.distributionmem) + count.distributionmem
          })
          commit('setZjBusinessQueryList', count)


          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    }
  },
  modules: {
    all
  }
})